define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

   
    
    var Controller = {
        index: function () {
            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

			var option = {
				title: {
					text: '10日订单数据统计'
				},
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'cross',
						label: {
							backgroundColor: '#6a7985'
						}
					}
				},
				legend: {
					data: ['订单数', '订单总额']
				},
				toolbox: {
					feature: {
						saveAsImage: {}
					}
				},
				grid: {
					left: '3%',
					right: '4%',
					bottom: '3%',
					containLabel: true
				},
				xAxis: [
					{
						type : 'category',
						boundaryGap: false,
						data : ['0']
					}
				],
				yAxis: [
					{
						type: 'value'
					}
				],
				series: [
					{
					    name:'订单数',
					    type:'line',
						stack: '总量',
					    data:[0],
					},
					{
					    name:'销售额',
					    type:'line',
						stack: '总量',
					    data:[0],
						label: {
							normal: {
								show: true,
								position: 'top'
							}
						},
					}
				]
			};

            //使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);

            //动态添加数据，可以通过Ajax获取数据然后填充
            setInterval(function () {
                if ($("#echart").width() != $("#echart canvas").width() && $("#echart canvas").width() < $("#echart").width()) {
                    myChart.resize();
                }
            }, 2000);

            setTimeout(function () {
				var city_id = $("#cityid").val();
				return ;
                $.post('https://newapp.lihua.com/api/order/getsevendays',{city_id : city_id}, function(ret){
                    console.log(ret);
                    if(ret.code == 1){
                        var newret = {};
                        ret.data.sevenData.forEach(function(val){
                            var keyarr = val.dt.split("-");
                            var key = keyarr[1]+"/"+keyarr[2];
                            newret[key] = {
								number : val.num,
								price : val.amount,
                            };
                        });

                        var curDate = new Date();//当前日期
                        var oneDay = 24*60*60*1000;
                        //近7天的日期
                        var nearSeven_Day = [];
                        var nearSeven_Number = [];
                        var nearSeven_Price = [];
                        for(var i=0; i<10; i++){
                            var t_date = new Date(curDate.getTime() - (9-i)*oneDay);
                            var getmonth = t_date.getMonth()+1;
                            var getdays = t_date.getDate();
                            if(getmonth<10){getmonth = "0"+getmonth};
                            if(getdays<10){getdays = "0"+getdays};
                            var dday = getmonth+"/"+getdays;
                            nearSeven_Day.push(dday);
                            if([dday] in newret){
								nearSeven_Number.push(newret[dday].number);
								nearSeven_Price.push(newret[dday].price);
                            }
                        }
						console.log(nearSeven_Number);
						console.log(nearSeven_Price);
						
                        myChart.setOption({
                            xAxis: {
                                data: nearSeven_Day
                            },
                            series: [
								{
									name:'订单数',
									type:'line',
									stack: '总量',
									data: nearSeven_Number,
									label: {
										normal: {
											show: true,
											position: 'top'
										}
									},
								},
								{
									name:'销售额',
									type:'line',
									stack: '总量',
									data: nearSeven_Price,
									label: {
										normal: {
											show: true,
											position: 'top'
										}
									},
								}
                            ]
                        });
                    }
                    
    
				})
				$.post("https://api.lihua.com/index/order/getTodayTotal", {}, function(ret){
					var new_amount=0 , old_amount=0;
					if(ret.status==0){
						old_amount = Math.floor(ret.data.data) || 0;
						new_amount = parseInt($("#todayAmount").html());
					}
					$("#toDayAllAmount").html(new_amount+old_amount);
				})
				
            }, 300);
            
            $(window).resize(function () {
                myChart.resize();
			});

			$(".showyeardata").click(function(){
				// return ;
				$.post("https://newapp.lihua.com/api/order/get_yeardata", {}, function(ret1){
					var d_2019 = 0;
					var d_2020 = 0;
					var d_2021 = 0;
					if(ret1.code==1){
						d_2019+= ret1.data.d_2019;
						d_2020+= ret1.data.d_2020;
						d_2021+= ret1.data.d_2021;
					}
					$.post("https://api.lihua.com/index/order/get_yeardata", {}, function(ret2){
						if(ret2.message=='OK'){
							d_2019+= ret2.data.d_2019;
							d_2020+= ret2.data.d_2020;
							d_2021+= ret2.data.d_2021;
						}
						d_2019 = d_2019 / 10000;
						d_2020 = d_2020 / 10000;
						d_2021 = d_2021 / 10000;
						$("#d_2019").html(d_2019.toFixed(2));
						$("#d_2020").html(d_2020.toFixed(2));
						$("#d_2021").html(d_2021.toFixed(2));
					})
				})
			})

            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });

            
        }
    };

    return Controller;
});